package controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.bcel.internal.generic.NEW;
import entity.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.*;
import utils.PageObject;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (KaoshitaskSetup 考试任务管理---考试任务设置)表控制层
 *
 * @author 张康泽
 * @since 2022-01-19 10:04:45
 */
@RestController
@RequestMapping("kaoshitaskSetup")
public class KaoshitaskSetupController {
    /**
     * 服务对象
     */
    @Resource
    private KaoshitaskSetupService kaoshitaskSetupService;

    @Resource
    private ExamService examService;

    @Resource
    private ClassroomTableService classroomTableService;

    @Resource
    private CurriculumListService curriculumListService;

    @Resource
    private GradetableService gradetableService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public KaoshitaskSetup selectOne(Integer id) {
        return this.kaoshitaskSetupService.queryById(id);
    }


    /**
     *考试任务设置查询所有数据并展示
     */
    @RequestMapping("/selectallbylimit")
    @CrossOrigin
    @ResponseBody
    public PageObject selectallbylimit(@RequestBody JSONObject object){
        PageObject pageObject = kaoshitaskSetupService.queryAllByLimit(object);
        return pageObject;
    }

    @RequestMapping("/cd")
    @CrossOrigin
    @ResponseBody
    public JSONObject cd(@RequestBody JSONObject object){
        JSONObject result = new JSONObject();
        List<exam> cd = examService.cd();
        if(cd.size()>0){
            result.put("code",0);
            result.put("data",cd);
        }else{
            result.put("code",1);
            result.put("msg","无冲突");
        }
        return result;
    }


    @RequestMapping("/xaunding")
    @CrossOrigin
    public boolean xaunding(@RequestBody JSONObject insertobj){
        return  kaoshitaskSetupService.xaunding(insertobj);

    }
    @RequestMapping("/delete")
    @CrossOrigin
    public boolean delete(@RequestBody JSONObject insertobj){
        return  kaoshitaskSetupService.deleteById(insertobj.getInteger("pkId"));

    }

    @RequestMapping("/saveCheckInfo")
    @CrossOrigin
    @ResponseBody
    public Object saveCheckInfo(@RequestBody JSONObject insertobj) throws Exception{
        JSONObject result = new JSONObject();
        JSONArray body = insertobj.getJSONArray("body");
        for (int i = 0; i < body.size(); i++) {
            JSONObject jsonObject = body.getJSONObject(i);
            try {
                JSONObject insert = examService.insert(jsonObject);
            }catch (Exception E){
                result.put("code", "-1");
                result.put("msg", "插入数据失败,请核对");
                return result;
            }
        }
        result.put("code", "0");
        result.put("msg", "数据插入成功");
        return result;
    };
    @RequestMapping("/findExamInfo")
    @CrossOrigin
    @ResponseBody
    public PageObject findExamInfo(@RequestBody JSONObject object){
        PageObject pageObject = examService.queryAllByLimit(object);
        return pageObject;
    }

    @RequestMapping("/getClassroom")
    @CrossOrigin
    @ResponseBody
    public List<ClassroomTable> getClassroom(){
        List<ClassroomTable> classroomTables = classroomTableService.queryClassroomAll();
        return classroomTables;
    }

    @RequestMapping("/getTheacherAndCourse")
    @CrossOrigin
    @ResponseBody
    public List<CurriculumList> getTheacher(){
        List<CurriculumList> curriculumLists = curriculumListService.getTheacherAndCourse();
        return curriculumLists;
    }

    @RequestMapping("/getClassname")
    @CrossOrigin
    @ResponseBody
    public List<Gradetable>  getClassname(){
        List<Gradetable> gradetables = gradetableService.queryClassnameAll();
        return gradetables;
    }


    @RequestMapping("/findByClassroomAndTeacherAndClassnameAndCourse")
    @CrossOrigin
    @ResponseBody
    public JSONObject  findByClassroomAndTeacherAndClassnameAndCourse(@RequestBody JSONObject object){
        List<exam> byClassroomAndTeacherAndClassnameAndCourse = null;
        JSONObject result = new JSONObject();
        Date tempDate_first = "".equals(object.getDate("date1"))?null:object.getDate("date1");
        Date tempDate_last = "".equals(object.getDate("date2"))?null:object.getDate("date2");
        String teacher = "".equals(object.getString("teacher"))?null:object.getString("teacher");
        String aClass = "".equals(object.getString("class"))?null:object.getString("class");
        String region = "".equals(object.getString("region"))?null:object.getString("region");
        String course = "".equals(object.getString("course"))?null:object.getString("course");
        try {
            byClassroomAndTeacherAndClassnameAndCourse = examService.findByClassroomAndTeacherAndClassnameAndCourse(tempDate_first,tempDate_last,teacher,aClass,region,course);
        }catch (Exception E){
            result.put("code", "-1");
            result.put("msg", "插入数据失败,请核对");
            return result;
        }
        result.put("code", "0");
        result.put("msg", "数据插入成功");
        result.put("data", byClassroomAndTeacherAndClassnameAndCourse);
        return result;
    }


}
